package gamedsl.game;

import gamedsl.event.Event;
import gamedsl.event.EventTracker;
import gamedsl.event.IEventHandler;

import org.newdawn.penguin.ActorPhysics;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;

/**
 * The example Alien actor running around. This is responsible for rendering
 * specifics for this game character. Everything physics related is handled
 * up in Actor.
 * 
 * @author kevin
 */
public class Alien extends ActorPhysics implements IEventHandler{
	/** The sprite sheet containing the running sprites */
	private SpriteSheet standingSprite;
	private SpriteSheet runningSprite;
	private SpriteSheet jumpingSprite;
	private SpriteSheet fallingSprite;
	/** The current frame of animation */
	private int frame;
	/** The timer controlling frame change */
	private int frameTimer = 0;
	/** The interval between animation frame change in milliseconds */
	private int frameInterval = 100;
	
	/**
	 * Create a new Alien actor read to add to the world
	 * 
	 * @param x The x position of the alien
	 * @param y The y position of the alien
	 * @param mass The mass of the alien
	 * @param size The size of the alien (collision size)
	 * @throws SlickException Indicates a failure to load resources for this alien
	 */
	public Alien (float x, float y, float mass, float size, String name, String type,int value) throws SlickException {
		super(x, y, mass, size, name, type, value);
	
			runningSprite = new SpriteSheet("res/running.png", 73, 57,2);
			jumpingSprite = new SpriteSheet("res/jumping3.png", 65, 70,2);
			fallingSprite = new SpriteSheet("res/falling.png", 60, 75,1);
			standingSprite = new SpriteSheet("res/standing.png", 62, 58,0);
		
			EventTracker.getInstance().subscribe("MushroomRewardHitEvent",this);
		
		
	}

	/**
	 * @see org.newdawn.penguin.Actor#render(org.newdawn.slick.Graphics)
	 */
	public void render(Graphics g) {
		// work out which animation we're showing based 
		// on the state of the actor
		SpriteSheet sheet = jumpingSprite;
		int sx = 0;
		int sy = 0;
		if (jumping()) {
			sheet = jumpingSprite;
			sx = 0;
			sy = 0;
		} else if (falling()) {
			sheet = fallingSprite;
			sx = 1;
			sy = 0;
		} else if (moving() && onGround()) {
			int f = (frame % 9) + 1;
			sheet = runningSprite;
			sx = f % 8;
			sy = 0;
 		} else if (onGround()) {
 			sheet = standingSprite;
			sx = 0;
			sy = 0;
		}
		
//		if (jumping()) {
//			sheet = jumpSprite;
//			sx = 0;
//			sy = 0;
//		} else if (falling()) {
//			sheet = jumpSprite;
//			sx = 2;
//			sy = 0;
//		} else if (moving() && onGround()) {
//			int f = (frame % 6) + 1;
//			sheet = runSprite;
//			sx = f % 3;
//			sy = f / 3;
// 		} else if (onGround()) {
//			sheet = runSprite;
//			sx = 0;
//			sy = 0;
//		}
		
		
		// get the appropriate sprite 
		Image image = sheet.getSprite(sx,sy);
		// if we're facing the other direction, flip the sprite over
//		if (facingRight()) {
//			image = image.getFlippedCopy(true, false);
//		}
		if (!facingRight()) {
			image = image.getFlippedCopy(true, false);
		}
		
		image.drawCentered(getX(), getY()-12);
		
		//g.drawString(""+onGround()+" "+getX()+"x"+getY()+" "+velx, getX(), getY()-50);
	}
	
	/**
	 * @see org.newdawn.penguin.Actor#preUpdate(int)
	 */
	public void preUpdate(int delta) {
		super.preUpdate(delta);
		
		frameTimer -= delta;
		while (frameTimer < 0) {
			frame++;
			frameTimer += frameInterval;
		}
	}

	@Override
	public void handleEvent(Event event) {
		// TODO Auto-generated method stub
		System.out.println("Player has hit " + event.getMessage());
	}

	@Override
	public int getHandlerId() {
		return super.getId();
	}

}

